<?php

namespace app\modules\clinical\controllers;

use Yii;
use app\modules\clinical\models;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use app\components\PtAccessControl;
use app\models\UploadForm;
use yii\web\UploadedFile;

/**
 * GeneController implements the CRUD actions for GeneInfo model.
 */
class GeneController extends Controller
{
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'pt_access' => [
                'class' => PtAccessControl::className(),
            ]
        ];
    }

    /**
     * Lists all GeneInfo models.
     * @return mixed
     */
    public function actionIndex()
    {
        $pt_id = Yii::$app->session->get('pt_id');
        $dataProvider = new ActiveDataProvider([
            'query' => models\GeneInfo::find()->where(['delete_at' => 0])->andWhere(['or', 'pt_id='.$pt_id, 'rel_id='.$pt_id]),
        ]);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
            'pt_rel_list' => models\Patient::findOne($pt_id)->getRelations(),
        ]);
    }

    /**
     * Displays a single GeneInfo model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
            'pt_rel_list' => models\Patient::findOne(Yii::$app->session->get('pt_id'))->getRelations(),
        ]);
    }

    /**
     * Creates a new GeneInfo model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate($id = 0)
    {
        $model = new models\GeneInfo();
        $pt_id = Yii::$app->session->get('pt_id');
        $pt_rel = Yii::$app->request->post('pt_rel', 0);

        $model->pt_id = ($pt_rel == 0) ? $pt_id : $pt_rel;//if pt_rel is 0, from the origin person.
        $model->rel_id = ($pt_rel == 0) ? 0 : $pt_id;
        $message = '';

        if ($model->load(Yii::$app->request->post())) {
            $model->rp_time = (preg_match('/___/', $model->rp_time)) ? '' : $model->rp_time;
            $model->disc_time = (preg_match('/___/', $model->disc_time)) ? '' : $model->disc_time;
            $model->recheck_time = (preg_match('/___/', $model->recheck_time)) ? '' : $model->recheck_time;
            $model->pdfFile = UploadedFile::getInstance($model, 'pdfFile');

            if (!is_null($model->pdfFile)) {
                if (!$model->upload()) {
                    $message = '文件上传失败，';
                }
            }

            if ($message == '' && $model->save()) {
                return $this->redirect(['/clinical/gene/view/', 'id' => $model->pk_id]);
            }

            $message = '保存失败';
        }

        return $this->render('create', [
            'model' => $model,
            'pt_rel_list' => $this->getRelatives(),
            'message' => $message,
        ]);
    }

    /**
     * Updates an existing GeneInfo model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post())) {
            $model->rp_time = (preg_match('/___/', $model->rp_time)) ? '' : $model->rp_time;
            $model->disc_time = (preg_match('/___/', $model->disc_time)) ? '' : $model->disc_time;
            $model->recheck_time = (preg_match('/___/', $model->recheck_time)) ? '' : $model->recheck_time;

            if ($model->save()) {
                return $this->redirect(['/clinical/gene/view/', 'id' => $id]);
            }

        }

        $pt_rel_list = models\Patient::findOne(Yii::$app->session->get('pt_id'))->getRelatives();
        return $this->render('update', [
            'model' => $model,
            'pt_rel_list' => $this->getRelatives(),
            'pt_rel' => ($model->rel_id != 0) ? $model->pt_id : 0,
        ]);
    }

    /**
     * Deletes an existing GeneInfo model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $model = $this->findModel($id);
        $model->delete_at = strtotime(date("Y-m-d H:i:s"));
        if ($model->save()) {
            return $this->redirect('/clinical/gene');
        }

        return $this->redirect('/clinical/gene/view?id='.$id);
    }

    /**
     * Finds the GeneInfo model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return GeneInfo the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = models\GeneInfo::findOne(['pk_id' => $id])) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

    protected function findGeneInfo($pt_id)
    {
        if (($model = models\GeneInfo::findOne(['pt_id' => $pt_id])) !== null) {
            return $model;
        } else {
            $model = new models\GeneInfo();
            $model->charge_name = '';
            $model->isSend = 0;
            $model->pt_recheck = 0;
            $model->pt_id = $pt_id;

            return $model;
        }
    }

    protected function getRelatives()
    {
        $rel = models\Patient::findOne(Yii::$app->session->get('pt_id'))->getRelatives();
        return $rel;
    }
}
